package com.sdkj.dspservice.modules.demand.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdkj.dspservice.common.base.entity.DataEntity;
import com.sdkj.dspservice.common.utils.StringUtils;
import com.sdkj.dspservice.modules.sys.entity.User;
import lombok.Data;

import java.util.Date;

/**
 * 需求表Entity
 *
 * @author tm
 */
@Data
@TableName(value = "demand_demand")
public class Demand extends DataEntity<Demand> {

    /**
     * 状态常量（0：已关闭   1：草稿（未开始流程，即没有详情）   2：处理中   3：已处理   4：已完成  5：退回）
     */
    public static final String STATUS_ClOSE = "0";
    public static final String STATUS_DRAFT = "1";
    public static final String STATUS_PROCESSING = "2";
    public static final String STATUS_PROCESSED = "3";
    public static final String STATUS_FINISHED = "4";
    public static final String STATUS_REJECT = "5";

    /**
     * 流程状态常量（0：已关闭  1：已提交  2.待审核  3.待分派  4.待处理  5.待完成  6.已完成  7.已退回）
     */
    public static final String FLOW_STATUS_ClOSE = "0";
    public static final String FLOW_STATUS_SUBMITTED = "1";
    public static final String FLOW_STATUS_VERIFY = "2";
    public static final String FLOW_STATUS_DISTRIBUTION = "3";
    public static final String FLOW_STATUS_PROCESS = "4";
    public static final String FLOW_STATUS_WAIT_FINISH = "5";
    public static final String FLOW_STATUS_FINISH = "6";
    public static final String FLOW_STATUS_REJECT = "7";


    /**
     * 名称
     */
    private String name;
    /**
     * 需求说明
     */
    private String description;
    /**
     * 预计工时（小时）
     */
    private Integer estimateWorkingHours;
    /**
     * 实际工时（小时）
     */
    private Integer actualWorkingHours;
    /**
     * 需求类型
     */
    private String typeId;
    /**
     * 当前流程用户
     */
    private String nowUserId;
    /**
     * 状态（0：已关闭   1：草稿   2：处理中   3：已处理   4：已完成  5：退回）
     */
    private String status;
    /**
     * 流程状态（0：已关闭  1：已提交  2.待审核  3.待分派  4.待处理  5.待完成  6.已完成  7.已退回）
     */
    private String flowStatus;
    /**
     * 预计完成时间
     */
    private Date estimateFinishDate;
    /**
     * 附件ids
     */
    private String annexIds;
    /**
     * 编号格式yyyyMMdd + 三位数
     */
    private String code;

    /**
     * 查询类型，0、全部需求  1、待办需求（当前流程用户为我）  2、流转中需求（除了待办、已完成）   3、已完成需求
     */
    @TableField(exist = false)
    private String findType = "0";
    /**
     * 是否保存时，同时发布，为1时，发布
     */
    @TableField(exist = false)
    private String isIssue = "0";
    /**
     * 以下用于参数传递方便
     */
    @TableField(exist = false)
    private DemandType type;
    @TableField(exist = false)
    private User nowUser;

    public Demand() {
        super();
    }

    public Demand(String id) {
        super(id);
    }

    public Demand(String status, String flowStatus) {
        this.status = status;
        this.flowStatus = flowStatus;
    }

    public String getStatusName() {
        String result = null;
        if (StringUtils.isNotBlank(status)) {
            switch (status) {
                case STATUS_ClOSE:
                    result = "已关闭";
                    break;
                case STATUS_DRAFT:
                    result = "草稿";
                    break;
                case STATUS_PROCESSING:
                    result = "处理中";
                    break;
                case STATUS_PROCESSED:
                    result = "已处理";
                    break;
                case STATUS_FINISHED:
                    result = "已完成";
                    break;
                case STATUS_REJECT:
                    result = "退回";
                    break;
                default:
                    result = null;
            }
        }
        return result;
    }

    public String getFlowStatusName() {
        String result = null;
        if (StringUtils.isNotBlank(flowStatus)) {
            switch (flowStatus) {
                case FLOW_STATUS_ClOSE:
                    result = "已关闭";
                    break;
                case FLOW_STATUS_SUBMITTED:
                    result = "已提交";
                    break;
                case FLOW_STATUS_VERIFY:
                    result = "待审核";
                    break;
                case FLOW_STATUS_DISTRIBUTION:
                    result = "待分派";
                    break;
                case FLOW_STATUS_PROCESS:
                    result = "待处理";
                    break;
                case FLOW_STATUS_WAIT_FINISH:
                    result = "待完成";
                    break;
                case FLOW_STATUS_FINISH:
                    result = "已完成";
                    break;
                case FLOW_STATUS_REJECT:
                    result = "已退回";
                    break;
                default:
                    result = null;
            }
        }
        return result;
    }

    /**
     * 把实体里的参数，放入查询中（只有关联查询需要这样，因为设置不了别名，只能手动插入）
     * @return
     */
    @Override
    public QueryWrapper<Demand> makeQueryWrapper(QueryWrapper<Demand> queryWrapper){
        if(StringUtils.isNotBlank(name)){
            queryWrapper.like("a.name", name);
        }
        if(StringUtils.isNotBlank(typeId)){
            queryWrapper.eq("a.type_id", typeId);
        }
        super.makeQueryWrapper(queryWrapper);
        return queryWrapper;
    }

}
